#include "stm32f10x_it.h"
#include "stdio.h"
 
extern u32 BufferSize ;
extern u8 I2C1_ADDRESS ;
extern u8 I2C2_ADDRESS ;
 
extern vu8 I2C1_Buffer_Tx[];
extern vu8 I2C2_Buffer_Rx[];
vu32 Tx_Counter = 0;
vu32 Rx_Counter = 0;
 
void i2c_test (void)
{ 	

	I2C_GenerateSTART(I2C1,ENABLE);
	
		I2C_Send7bitAddress(I2C1,I2C2_ADDRESS,I2C_Direction_Transmitter);	

		I2C_SendData(I2C1,I2C1_Buffer_Tx[Tx_Counter++]);
		if(Tx_Counter<BufferSize)
		{
			printf("\r\n The I2C1 has send data %d \r\n",I2C1_Buffer_Tx[Rx_Counter]);
			I2C_SendData(I2C1,I2C1_Buffer_Tx[Tx_Counter++]);   				

		}else{
			I2C_GenerateSTOP(I2C1,ENABLE);
			//I2C_ITConfig(I2C1,I2C_IT_EVT|I2C_IT_BUF,DISABLE);  //计数发送的个数
		}
}




 发送不需要接受数据，只是单纯发送
void I2C1_EV_IRQHandler(void)
{
	switch(I2C_GetLastEvent(I2C1))
	{
		case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: //收到匹配的地址数据
		{
			break;
		}
		case I2C_EVENT_SLAVE_BYTE_RECEIVED: //收到数据
		{	
			if(Rx_Counter < BufferSize )
			{
				I2C2_Buffer_Rx[Rx_Counter] = I2C_ReceiveData(I2C1);
				printf("\r\n The I2C1 has received data %d \r\n",I2C2_Buffer_Rx[Rx_Counter++]); //计数收到的个数				
			}
			break;
		}
		case I2C_EVENT_SLAVE_STOP_DETECTED: //收到结束条件
		{
			I2C_ClearFlag(I2C1,I2C_FLAG_STOPF);
			//I2C_ITConfig(I2C1,I2C_IT_EVT|I2C_IT_BUF,DISABLE);
 
			break;
		}
		default: {break;}
	}
 
 接受到数据保存打印出来
void I2C2_EV_IRQHandler(void)
{
	switch(I2C_GetLastEvent(I2C2))
	{
		case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: //收到匹配的地址数据
		{
			break;
		}
		case I2C_EVENT_SLAVE_BYTE_RECEIVED: //收到数据
		{	
			if(Rx_Counter < BufferSize )
			{
				I2C2_Buffer_Rx[Rx_Counter] = I2C_ReceiveData(I2C2);
				printf("\r\n The I2C2 has received data %d \r\n",I2C2_Buffer_Rx[Rx_Counter++]); //计数收到的个数				
			}
			break;
		}
		case I2C_EVENT_SLAVE_STOP_DETECTED: //收到结束条件
		{
			I2C_ClearFlag(I2C2,I2C_FLAG_STOPF);
			//I2C_ITConfig(I2C1,I2C_IT_EVT|I2C_IT_BUF,DISABLE);
 
			break;
		}
		default: {break;}
	}
}
 
/////////////////////////////////////////////////////////////////////////////
 方案二试试
 
 主函数
 
	while(1){
		
		 	I2C_GenerateSTART(I2C1,ENABLE);
Delay_s(1);

		
		
	}
 
 
 #include "stm32f10x_it.h"
#include "stdio.h"
 
extern u32 BufferSize ;
extern u8 I2C1_ADDRESS ;
extern u8 I2C2_ADDRESS ;
 
extern vu8 I2C1_Buffer_Tx[];
extern vu8 I2C2_Buffer_Rx[];
vu32 Tx_Counter = 0;
vu32 Rx_Counter = 0;
 
void I2C1_EV_IRQHandler(void)
{
	switch(I2C_GetLastEvent(I2C1))
	{
		case I2C_EVENT_MASTER_MODE_SELECT: //已发送启始条件
		{
			I2C_Send7bitAddress(I2C1,I2C2_ADDRESS,I2C_Direction_Transmitter);	
			break;
		}
		case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED: //已发送从机地址
		{
			printf("\r\n The I2C1 has send data %d \r\n",I2C1_Buffer_Tx[Rx_Counter]);
			I2C_SendData(I2C1,I2C1_Buffer_Tx[Tx_Counter++]);
			break;
		}
		case I2C_EVENT_MASTER_BYTE_TRANSMITTED: //第一个数据已发送
		{
			if(Tx_Counter<BufferSize)
			{
				printf("\r\n The I2C1 has send data %d \r\n",I2C1_Buffer_Tx[Rx_Counter]);
				I2C_SendData(I2C1,I2C1_Buffer_Tx[Tx_Counter++]);   				
 
			}else{
				I2C_GenerateSTOP(I2C1,ENABLE);
				Tx_Counter=0;
				//I2C_ITConfig(I2C1,I2C_IT_EVT|I2C_IT_BUF,DISABLE);  //计数发送的个数
			}
 
			break;
		}
		default: {break;}
	}
}
 
 
void I2C2_EV_IRQHandler(void)
{
	switch(I2C_GetLastEvent(I2C2))
	{
		case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: //收到匹配的地址数据
		{
			break;
		}
		case I2C_EVENT_SLAVE_BYTE_RECEIVED: //收到数据
		{	
			if(Rx_Counter < BufferSize )
			{
				I2C2_Buffer_Rx[Rx_Counter] = I2C_ReceiveData(I2C2);
				printf("\r\n The I2C2 has received data %d \r\n",I2C2_Buffer_Rx[Rx_Counter++]); //计数收到的个数				
			}
			break;
		}
		case I2C_EVENT_SLAVE_STOP_DETECTED: //收到结束条件
		{
			I2C_ClearFlag(I2C2,I2C_FLAG_STOPF);
			Rx_Counter = 0;
			//I2C_ITConfig(I2C1,I2C_IT_EVT|I2C_IT_BUF,DISABLE);
 
			break;
		}
		default: {break;}
	}
}
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 